package xyx.springboot.controller;

import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyx.springboot.model.User;
import xyx.springboot.service.JpaService;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController //返回数据
@Api(tags={"Jpa测试"},description = "使用jpa查询")
public class JpaTestController {

    @Autowired
    JpaService jpaService;

    @ApiOperation(value = "查询",notes = "查询所有用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询用户成功")})
    @PostMapping(value = "/selectAll")
    public JSONObject selectAll(){
        JSONObject jsonObject = new JSONObject();
        List list = jpaService.listAll();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @ApiOperation(value = "查询",notes = "根据id查询用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询用户成功")})
    @PostMapping(value = "/selectById")
    public JSONObject selectById(@ApiParam(value = "用户id",required = true) @RequestParam(value = "id", required = true) int id){
        JSONObject jsonObject = new JSONObject();
        Optional optional = jpaService.findById(id);
        User user = jpaService.getById(id);
        jsonObject.put("optional",optional);
        jsonObject.put("user",user);
        return jsonObject;
    }

    @ApiOperation(value = "查询",notes = "自定义根据id查询用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询用户成功")})
    @PostMapping(value = "/myfind")
    public JSONObject myfind(@ApiParam(value = "用户id",required = true) @RequestParam(value = "id", required = true) int id){
        JSONObject jsonObject = new JSONObject();
        Map map = jpaService.myfind(id);
        jsonObject.put("map",map);
        return jsonObject;
    }

    @ApiOperation(value = "新增/修改",notes = "新增/修改用户（用户id为null时新增，不为null时更新）")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "用户新增/修改成功")})
    @PostMapping(value = {"/update","/save"})
    public JSONObject save(@ApiParam(value = "用户id",required = false) @RequestParam(value = "id", required = false) Integer id,
                           @ApiParam(value = "用户名",required = false) @RequestParam(value = "username", required = false) String username,
                           @ApiParam(value = "密码",required = false) @RequestParam(value = "password", required = false) String password,
                           @ApiParam(value = "真实姓名",required = false) @RequestParam(value = "realname", required = false) String realname,
                           @ApiParam(value = "联系方式",required = false) @RequestParam(value = "phonumber", required = false) String phonumber) {
        JSONObject jsonObject = new JSONObject();
        User user = new User();
        if(id != null){
            user.setId(id);
            jsonObject.put("type","修改");
        }
        else{
            jsonObject.put("type","新增");
        }
        user.setUsername(username);
        user.setPassword(password);
        user.setRealname(realname);
        user.setPhonumber(phonumber);
        jpaService.save(user);
        jsonObject.put("user",user);
        return jsonObject;
    }

}
